<script>
	export default {
		data () {
			return {
				publicKeyUrl: 'publicKey.php',
				params: '',
				loginState: 'loginState.php',
				loginInfo: {
					User: '',
					Password: '',
					Token: ''
				}
			}
		},
		onLaunch: function() {
			console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			async api (url, params, type) {
				const { data: res } = await this.$myHttp({ // 获取公钥
					url: url,
					data: params
				})
				// console.log(res)
				if(type === 'deposit') {
					if (res.data === '' || res.data === undefined || res.data === null) {
						uni.showToast({
							title: res.Msg,
							icon: 'loading',
							duration: 3000,
							mask: true
						})
					}
					this.$store.commit('getPublicKey', res.data) // 保存公钥
					this.login()
				} else if (type === 'stote') {
					if (res.Code === 200) {
						// 保存到状态管理
						uni.showToast({
							title: res.Msg,
							icon: 'loading',
							duration: 3000,
							mask: true
						})
					} else {
						uni.showToast({
							title: res.Msg,
							icon: 'loading',
							duration: 3000,
							mask: true
						})
						uni.reLaunch({
							url: '/pages/Login/Login'
						})
					}
				}
			},
			publicKey () {
				this.api(this.publicKeyUrl, this.params, 'deposit')
			},
			login() {
				let that = this
				const Pro = new Promise(function (resolve, reject) {
					uni.getStorage({
						key: 'Token',
						success (z) {
							resolve(z)
						},
						fail (e) {
							resolve(e)
						}
					})
				})
				Pro.then(function (res) {
					// 登录状态
					if (res.data === '') {
						uni.redirectTo({
							url: '/pages/Login/Login'
						})
					} else {
						//#ifdef APP-PLUS
						that.$store.commit('getLoginInfo', res.data)
						//#endif
						// 验证登录状态
						that.api(that.loginState, res.data, 'stote')
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}
				 })
			}
		},
		created() {
			this.publicKey();
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
